package com.leancloud.login.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.aries.ui.view.title.TitleBarView;
import com.leancloud.login.LoginPath;
import com.leancloud.login.R;

import tech.com.commoncore.LoginInterceptorImpl;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.utils.ToastUtil;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

@Route(path = LoginPath.LOGIN_CHANGEPWD, extras = LoginInterceptorImpl.LOGINCHECK)
public class ChangePwdActivity extends BaseTitleActivity {
    private EditText oldPasswordEt, passwordEt, confirmEt;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTextColor(Color.WHITE)
                .setLeftTextDrawable(null)
                .setBgColor(Color.TRANSPARENT)
                .setDividerHeight(0)
                .setStatusBarLightMode(true)
                .setRightTextDrawable(R.drawable.login_icon_back)
                .setOnRightTextClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
    }

    @Override
    public int getContentLayout() {
        return R.layout.login_activity_password_change;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        ((TextView) findViewById(R.id.input_banner_tx)).setText("密码修改");
        oldPasswordEt = findViewById(R.id.login_input_old_pwd_et);
        passwordEt = findViewById(R.id.login_input_password_et);
        confirmEt = findViewById(R.id.login_input_confirm_et);

        findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changePassword();
            }
        });

        setToggleEffect();
    }


    private void setToggleEffect() {
        ((ToggleButton) findViewById(R.id.old_password_toggle))
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        setPasswordShowOrHide(oldPasswordEt, isChecked);
                    }
                });
        ((ToggleButton) findViewById(R.id.password_toggle))
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        setPasswordShowOrHide(passwordEt, isChecked);
                    }
                });
        ((ToggleButton) findViewById(R.id.confirm_toggle))
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        setPasswordShowOrHide(confirmEt, isChecked);
                    }
                });
    }

    private void setPasswordShowOrHide(EditText editText, boolean checked) {
        if (checked) {
            editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
    }

    private void changePassword() {
        String oldPwd = oldPasswordEt.getText().toString().trim();
        String password = passwordEt.getText().toString().trim();
        String confirm = confirmEt.getText().toString().trim();

        if (oldPwd == null || oldPwd.isEmpty()) {
            ToastUtil.show("请输入原始密码");
            return;
        }

        if (password == null || password.isEmpty()) {
            ToastUtil.show("请输入原始密码");
            return;
        }

        if (confirm == null || confirm.isEmpty() || !confirm.equals(password)) {
            ToastUtil.show("密码不一致");
            return;
        }
        BmobUser.updateCurrentUserPassword(oldPwd, password, new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    ToastUtil.show("密码更改成功");
                    ChangePwdActivity.this.finish();
                } else {
                    ToastUtil.show("旧密码不正确");
                }
            }
        });
    }
}
